package com.qeeyou.apps.android.craftsman.service;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.qeeyou.apps.android.craftsman.activity.login.LoginActivity;

/**
 * QA相关服务
 * @author EsaFans
 *
 */
public class CommonErrorHandler {
	
	/**
	 * 单例
	 */
	private static CommonErrorHandler instance;
	
	/**
	 * 单例方法
	 * @return
	 */
	public static CommonErrorHandler getInstance() {
		if(instance==null){
			instance = new CommonErrorHandler();
		}
		return instance;
	}
		
	private CommonErrorHandler() {
		
	}
	
	public boolean isError(String errorCode, String errorMsg) {
		if(errorCode == null) {
			return false;
		}
		if(!errorCode.equalsIgnoreCase("1")) {
			handlerError(errorCode, errorMsg);
			return true;
		}
		return false;
	}
	
	
	public void handlerError(String errorCode, String errorMsg) {
		handler.sendMessage(handler.obtainMessage(HANDLER_LOOPMSG,
				new handlerMessageObj(errorCode,errorMsg)));
	}
	
	
	private String getErrorMsgCustom(String errorMsg){
		return (errorMsg == null || errorMsg.trim().equalsIgnoreCase("")) ? "" : ("" + errorMsg);
	}

	public class handlerMessageObj {
		public String errorCode;
		public String errorMsg;
		public handlerMessageObj(String errorCode, String errorMsg) {
			this.errorCode = errorCode;
			this.errorMsg = errorMsg;
		}
	}
	
	private int HANDLER_LOOPMSG = 41233;

	private Handler handler = new Handler(Looper.getMainLooper()){
		
		public void handleMessage(Message msg) {
			
			if(msg.what == HANDLER_LOOPMSG) {
				handlerMessageObj msgObj = (handlerMessageObj)msg.obj;
				String errorCode = msgObj.errorCode;
				String errorMsg = msgObj.errorMsg;
				if(errorCode.equalsIgnoreCase("-1")) {
					//token失效
//					Intent intent = new Intent(SystemContext.getInstance().getContext(), LoginActivity.class);
//					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//					SystemContext.getInstance().getContext().startActivity(intent);
				} else {

				}
			}
		};
	};
	
	
	
}
